﻿/***
公司信息表
***/
Ext.define('PT.view.company.CompanysPanel', {
    extend: 'Ext.panel.Panel',
    ids: null,
    viewConfig: {
        stripeRows: true
    },
    initComponent: function () {

        var me = this;

        var gridstore = Ext.create('Ext.data.Store', {
            fields: [
                    { name: 'id', type: 'int' },
                     'title', 'address', 'province', 'city', 'url', 'tele', 'email', 'remarks', 'ctype', 'createDate', 'lnkUser', 'privilege'
            ],
            proxy: {
                type: 'ajax',
                url: 'Company',
                reader: {
                    type: 'json',
                    root: 'rows'
                }
            }
        });

        var loadData = function () {

            var form = searchForm.getForm();

            //var mobileNo = form.findField("txt_phone").getValue();

            var temp = form.findField("txt_begin_date").value;
            var begin_date = Ext.Date.format(temp, 'Ymd');

            temp = form.findField("txt_begin_time").getValue();
            var begin_time = Ext.Date.format(temp, 'His');

            temp = form.findField("txt_end_date").getValue();

            var end_date = Ext.Date.format(temp, 'Ymd');

            temp = form.findField("txt_end_time").getValue();
            var end_time = Ext.Date.format(temp, 'His');

            gridstore.on('beforeload', function (store, options) {

                var extraParams = {
                    //MobileNum: mobileNo,
                    TestBeginTime: begin_date + begin_time,
                    TestEndTime: end_date + end_time
                };

                Ext.apply(store.proxy.extraParams, extraParams);

            });

            gridstore.load({
                params: {
                    start: 0,
                    limit: 25
                }
            });

        };
               
        var cityCodeStore = Ext.create('PT.store.CityCodeStore');
        var ProvCodeStore = Ext.create('PT.store.ProvCodeStore');

        var searchForm = Ext.create('Ext.form.Panel', {
            region: 'north',
            fieldDefaults: {
                msgTarget: 'side',
                labelAlign: 'right',
                labelWidth: 105
            },
            defaults: {
                anchor: '100%'
            },
            items: [{
                collapsible: true,
                collapsed: false,
                bodyStyle: 'padding:5px',
                title: '检索条件',
                defaultType: 'textfield',
                layout: 'anchor',
                defaults: {
                    anchor: '50%'
                },
                items: [
                    {
                        fieldLabel: '<span style="color: #f00">*</span>开始时间',
                        xtype: 'fieldcontainer',
                        anchor: '100%',
                        layout: 'hbox',
                        items: [{
                            xtype: 'datefield',
                            name: 'txt_begin_date',
                            format: 'Y-m-d',
                            value: new Date(),
                            allowBlank: false
                        }, {
                            xtype: 'timefield',
                            name: 'txt_begin_time',
                            format: 'H:i:s',
                            value: '00:00',
                            allowBlank: false
                        }]
                    },
                {
                    fieldLabel: '<span style="color: #f00">*</span>结束时间',
                    xtype: 'fieldcontainer',
                    anchor: '100%',
                    layout: 'hbox',
                    items: [{
                        xtype: 'datefield',
                        name: 'txt_end_date',
                        format: 'Y-m-d',
                        value: new Date(),
                        allowBlank: false
                    }, {
                        xtype: 'timefield',
                        name: 'txt_end_time',
                        format: 'H:i:s',
                        value: '23:59',
                        allowBlank: false
                    }]
                },
                {
                    xtype: 'container',
                    layout: 'column',
                    anchor: '0',
                    items: [{
                        xtype: 'container',
                        columnWidth: 0.25,
                        items: [{
                            //xtype: 'combobox',
                            xtype: 'textfield',
                            fieldLabel: ' 厂家',
                           // store: venderCodeStore,
                           // queryMode: 'local',
                           // displayField: 'value',
                           // valueField: 'key',
                           // editable: false,
                            name: 'txt_venderCode'
                        }]
                    }, {
                        xtype: 'container',
                        columnWidth: 0.25,
                        items: [{
                            xtype: 'textfield',
                            fieldLabel: '终端型号',
                            maxLength: 11,
                            name: 'txt_devType'
                        }]
                    }, {
                        xtype: 'container',
                        columnWidth: 0.25,
                        items: [{
                            xtype: 'combobox',
                            fieldLabel: '省份',
                            store: ProvCodeStore,
                            queryMode: 'local',
                            displayField: 'value',
                            valueField: 'key',
                            editable: false,
                            name: 'txt_provCode',
                            listeners: {
                                'change': function (m, newValue, oldValue, eOpts) {

                                    var form = searchForm.getForm();
                                    form.findField('txt_city').setValue('');

                                    cityCodeStore.clearFilter(true);
                                    cityCodeStore.filter([
                                            { property: "refcode", value: newValue }
                                    ]);
                                }
                            }
                        }]
                    }, {
                        xtype: 'container',
                        columnWidth: 0.25,
                        items: [{
                            xtype: 'combobox',
                            fieldLabel: ' 城市',
                            store: cityCodeStore,
                            queryMode: 'local',
                            displayField: 'value',
                            editable: false,
                            valueField: 'key',
                            name: 'txt_city'
                        }]
                    }]
                }],
                buttons: [{
                    text: '检索',
                    handler: function () {

                        loadData();
                    }
                }, {
                    text: '清空',
                    handler: function () {

                        var form = this.up('form').getForm();

                        form.reset();
                        loadData();
                    }
                }]
            }]
        });

        Ext.applyIf(me, {
            layout: 'border',
            items: [searchForm,
				{
				    xtype: 'gridpanel',
				    region: 'center',
				    store: gridstore,
				    selModel: Ext.create('Ext.selection.CheckboxModel', { mode: 'SIMPLE' }),
				    columns: [
    				{ header: '公司名称', dataIndex: 'title' },
    				{ header: '类别', dataIndex: 'ctype' },    				
    				{ header: '电话', dataIndex: 'tele', hidden: false },    				
                    { header: 'Email', dataIndex: 'email' },
                    { header: '地址', dataIndex: 'address' ,flex:1},
                    {
        		        header: '创建时间', dataIndex: 'createDate',flex:1, renderer: function (v) {

        		            if (v != null && v.length == 21) {

        		                var d = new Date(parseInt(v.substr(6, 13)));

        		                return Ext.Date.format(d, 'Y-m-d H:i:s');

        		            } else {
        		                return v;
        		            }
        		        }
                    },
                    {
                        xtype: 'actioncolumn',
                        flex: 1,
                        items: [{
                            icon: 'images/icons/fam/information.png',
                            tooltip: '查看',
                            handler: function (grid, rowIndex, colIndex) {
                                var rec = grid.getStore().getAt(rowIndex);

                                Ext.create('PT.view.window.EditCompanyWindow', {
                                    rec: rec,
                                    listeners: {
                                        'beforedestroy': function () {
                                            loadData();
                                        }
                                    }
                                }).show();

                            }
                        }
                        ]
                    }
				    ],
				    dockedItems: [{
				        xtype: 'toolbar',
				        dock: 'top',
				        items: [{
				            text: '添加',
				            tooltip: '添加客户',
				            iconCls: 'add',
				            handler: function () {

				                Ext.create('PT.view.window.EditCompanyWindow', {
				                    listeners: {
				                        'beforedestroy': function () {
				                            gridstore.load({ params: { taskType: 0 } });
				                        }
				                    }
				                }).show();

				            }
				        }, {
				            text: '删除',
				            tooltip: '删除客户',
				            iconCls: 'del',
				            handler: function () {

				            }
				        }, '-', {
				            xtype: 'textfield',
				            width: 300,
				            maxLength: 11,
				            name: 'txt_key'
				        }, {
				            text: '查询',
				            tooltip: '根据关键字查询结果',
				            iconCls: 'search',
				            handler: function () {

				            }
				        }]
				    },
				        {
				            xtype: 'pagingtoolbar',
				            store: gridstore,
				            dock: 'bottom',
				            displayInfo: true
				        }
				    ]
				}
            ]
        });
        me.callParent(arguments);


    }
});